<template>
  <div class="table-box">
    <ProTable ref="proTable" :columns="columns" :request-api="getAdvPositionList">
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <el-button v-auth="'adv_position_save'" type="primary" :icon="CirclePlus" @click="openDrawer('新增')">
          新增广告位
        </el-button>
      </template>
      <!-- 表格操作 -->
      <template #operation="scope">
        <el-button v-auth="'adv_position_save'" type="primary" link :icon="EditPen" @click="openDrawer('编辑', scope.row)">
          编辑
        </el-button>
        <el-button v-auth="'adv_position_del'" type="danger" link :icon="Delete" @click="deleteAdvPosition(scope.row)">
          删除
        </el-button>
      </template>
    </ProTable>
    <AdvPositionEdit ref="drawerRef" />
  </div>
</template>
<script setup lang="tsx" name="advPosition">
import { ref, reactive } from "vue";
import { Market } from "@/api/interface";
import ProTable from "@/components/ProTable/index.vue";
import AdvPositionEdit from "@/views/market/advposition/AdvPositionEdit.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import { CirclePlus, Delete, EditPen } from "@element-plus/icons-vue";
import { getAdvPositionList, saveAdvPosition, delAdvPosition } from "@/api/modules/market";
import { useHandleData } from "@/hooks/useHandleData";
// ProTable 实例
const proTable = ref<ProTableInstance>();

// 表格配置项
const columns = reactive<ColumnProps<Market.AdvPosition>[]>([
  { prop: "name", label: "广告位名称", search: { el: "input" } },
  { prop: "code", label: "广告位标识" },
  { prop: "width", label: "宽度" },
  { prop: "height", label: "高度" },
  { prop: "createdAt", label: "创建时间" },
  { prop: "updatedAt", label: "更新时间" },
  { prop: "operation", label: "操作", width: 250, fixed: "right" }
]);

// 删除权限资源
const deleteAdvPosition = async (params: Market.AdvPosition) => {
  await useHandleData(delAdvPosition, { codes: params.code }, `删除【${params.name}】广告位`);
  proTable.value?.getTableList();
};

// 打开 drawer(新增、查看、编辑)
const drawerRef = ref<InstanceType<typeof AdvPositionEdit> | null>(null);
const openDrawer = (title: string, row: Partial<Market.AdvPosition> = {}) => {
  const params = {
    title,
    isView: title === "查看",
    row: { ...row },
    api: saveAdvPosition,
    getTableList: proTable.value?.getTableList
  };
  drawerRef.value?.acceptParams(params);
};
</script>
